package com.hee.erp.modules.controller;

import com.hee.erp.common.R;
import com.hee.erp.constant.ReturnCode;
import com.hee.erp.enums.AuditStatusEnum;
import com.hee.erp.modules.entity.AuditFlow;
import com.hee.erp.modules.service.IAuditFlowService;
import com.hee.erp.vo.leave.AuditFlowNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/modules/auditFlow")
@Api(tags = "审核流程")
public class AuditFlowController {

    @Autowired
    IAuditFlowService auditFlowService;

    @PostMapping("/audit")
    @ApiOperation("审核通过或驳回")
    public R audit(@RequestBody AuditFlow auditFlow) {
        if(StringUtils.isBlank(auditFlow.getId())){
            return R.fail(ReturnCode.RC406.getCode(),"id不能为空");
        }
        if(StringUtils.isBlank(auditFlow.getAuditorUserId())){
            return R.fail(ReturnCode.RC406.getCode(),"审核人id不能为空");
        }
        if(auditFlow.getAuditStatus().equals(AuditStatusEnum.REJECT)){
            //如果是驳回，则需要填写驳回原因
            if(StringUtils.isBlank(auditFlow.getAuditRemark())){
                return R.fail(ReturnCode.RC406.getCode(),"驳回原因不能为空");
            }
        }
        if(auditFlow.getAuditStatus().equals(AuditStatusEnum.PASS)){
            //如果是通过，则需要填写审核意见
            if(StringUtils.isBlank(auditFlow.getAuditRemark())){
                auditFlow.setAuditRemark("同意！");
            }
        }
        auditFlow.setAuditTime(LocalDateTime.now());
        auditFlowService.audit(auditFlow);
        return R.success(true);
    }
}
